//
//  XLIDScanViewController.m
//  IDAndBankCard
//
//  Created by  on 2017/3/28.
//  Copyright © 2017年 mxl. All rights reserved.
//

#import "XLIDScanViewController.h"
#import "IDOverLayerView.h"

@interface XLIDScanViewController ()

@property (nonatomic, strong) IDOverLayerView *overlayView;

@end

@implementation XLIDScanViewController

-(IDOverLayerView *)overlayView {
    if(!_overlayView) {
        CGRect rect = [IDOverLayerView getOverlayFrame:[UIScreen mainScreen].bounds];
        _overlayView = [[IDOverLayerView alloc] initWithFrame:rect];
    }
    return _overlayView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    __weak XLIDScanViewController * weakSelf = self;
    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:nil leftAction:^(UIButton * sender){
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } rightAction:nil];
    navView.backgroundColor = [UIColor clearColor];
    [self.view insertSubview:self.overlayView atIndex:0];
    
    self.cameraManager.sessionPreset = AVCaptureSessionPresetHigh;
    
    if ([self.cameraManager configIDScanManager]) {
        UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
        [self.view insertSubview:view atIndex:0];
        AVCaptureVideoPreviewLayer *preLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.cameraManager.captureSession];
        preLayer.frame = [UIScreen mainScreen].bounds;
        
        preLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        [view.layer addSublayer:preLayer];
        
        [self.cameraManager startSession];
    }
    else {
        NSLog(@"打开相机失败");
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    __weak XLIDScanViewController * weskSelf = self;
    self.cameraManager.finishBlock = ^(XLScanResultModel *model) {
        NSLog(@"model------>%@",model);
        if(weskSelf.finishBlock){
            weskSelf.finishBlock(model);
        }
        [weskSelf.navigationController popViewControllerAnimated:YES];
    };
}
-(void)dealloc{
    NSLog(@"%s",__PRETTY_FUNCTION__);
}


@end
